package files

import (
	"bytes"
	"com.lh.basic.service/config"
	"errors"
	"fmt"
	"path/filepath"
	"text/template"
)

// prefix 为项目名
// name 为目标html文件
func (t *Template) GetMailHtml(prefix string, name string, data map[string]string) (string, error) {
	root := config.GetPath("root")
	tpl := template.New(name)
	var html bytes.Buffer
	path := fmt.Sprintf("%s/%s/lib/template/%s", *root, prefix, name)
	if filepath.Ext(name) == ".html" {
		if _, err := tpl.ParseFiles(path); err != nil {
			return html.String(), err
		}
		if err := tpl.ExecuteTemplate(&html, name, data); err != nil {
			return html.String(), err
		}
		return html.String(), nil
	}
	return html.String(), errors.New("模板路径错误")
}
